import React from 'react'
import { Icon, message, Upload } from 'antd'
const getUploadUrl = (env, type) => {
  let prefix = 'http://staging.'
  if (env === '/learn') {
    prefix = 'https://'
  }
  return `${prefix}school.thoughtworks.cn/learn/zuul/program-center/api/` + (type === 'video' ? 'uploadVideos' : 'uploadImages')
}

const UploadComponent = ({uploadSuccess, env, type}) => {
  const url = getUploadUrl(env, type)
  const maxSize = () => {
    return type === 'video' ? 1024 : 10
  }
  const props = {
    name: 'file',
    accept: type === 'video' ? 'video/*' : 'image/*',
    action: url,
    beforeUpload (file) {
      const isLimit = file.size / 1024 / 1024 < (maxSize())
      if (!isLimit) {
        message.error(`只能上传小于${maxSize()}MB!`)
        return false
      }
    },
    onChange (info) {
      if (info.file.status === 'done') {
        message.success(`${info.file.name} file uploaded successfully`)
        uploadSuccess(info.file.response)
      } else if (info.file.status === 'error') {
        message.error(`${info.file.name} file upload failed.`)
      }
    }

  }

  return (
    <Upload {...props}>
      <Icon className='react-markdown-icon' type='cloud-upload' theme='outlined' />
    </Upload>
  )
}

export default UploadComponent
